package cj.web.marriage.controllers;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import cj.utils.Constants;
import cj.web.admin.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.dao.VoteDAO;
import cj.web.marriage.domain.Vote;

@Path("/vote")
public class VoteController extends BaseController{

	private static Logger logger = Logger.getLogger(VoteController.class.getName());
	
	@Autowired
	private VoteDAO voteDao;
	
	@Get("show")
	public String show(Model model,Vote vote) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.vote);
		return "/views/marriage/voteList.vm";
	}

	@Post("getList")
	public String queryList(Vote vote) {
		JSONObject jo = new JSONObject();
		List<Vote> objs = voteDao.query(vote);
		int size = voteDao.querySize(vote);
		jo.put("data", objs);
		jo.put("draw", vote.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model){
		//MemuEnum
        model.add("MemuEnum",MemuEnum.vote);
		return "/views/marriage/voteDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Vote vote,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			vote.setCreateUser(returnSessionObj(request).getAccount());
			vote.setUpdateUser(returnSessionObj(request).getAccount());
			voteDao.create(vote);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		Vote obj = voteDao.queryById(id);
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.vote);
		return "/views/marriage/voteEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Vote vote,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			vote.setUpdateUser(returnSessionObj(request).getAccount());
			voteDao.update(vote);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		voteDao.deleteById(id);
		return "r:show";
	}
}